package com.jlsoft;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.jlsoft.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@SpringBootApplication
@Controller
@Slf4j
public class IWaterAplication {

	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(IWaterAplication.class);
		application.setBannerMode(Banner.Mode.OFF);
		application.run(args);

	}

	@Autowired
    private IUserService userService;




	@Autowired
    private Producer captchaProducer;

    @RequestMapping(value = "/captcha")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        //生成验证码
        String capText = captchaProducer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        //向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }


	@RequestMapping("/")
	public String sayMethod() {

        //分页查询
//		PageHelper.startPage(1, 5);
//        List<User> userList =  userMapper.findAll();
//		log.debug("findAll:{}", JSON.toJSONString(userList));

		//分页查询封装结果
//        PageInfo<User> pageInfo = new PageInfo<User>();

//        PageInfo<User> page = PageHelper.startPage(1,10).doSelectPageInfo(()->userMapper.findAll());
//
//        log.debug("findAll:{}", JSON.toJSONString(page));

//
//        List<User> userList2 =  userMapper.findAll();
//        log.debug("findAll2:{}", JSON.toJSONString(userList2));
//

//        List<Map<String, Object>> userListMap =  userMapper.findAllToMap();
//        log.debug("findAll:{}", JSON.toJSONString(userListMap));

//		List<User> userList = userMapper.findAllById(1L);
//		log.debug("findAllById:{}", JSON.toJSONString(userList));

//        List<User> userList = userMapper.findAllByIdAndFlag(1L, false);
//		log.debug("findAllById:{}", JSON.toJSONString(userList));

//        Map<Long, User> userList = userMapper.findAllToMapUser();
//		log.debug("findAllById:{}", JSON.toJSONString(userList));




		return "views/index";
	}

    @RequestMapping("/view/personInfo")
    public String viewPersonInfo() {

        return "views/app/personInfo";
    }



        @RequestMapping("/webme")
    @ResponseBody
    public Object currentWebUser(@AuthenticationPrincipal UserDetails userDetails) {

        //UserDetails userDetailss = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return userDetails;
    }
}
